package com.content.scaffold.common.web;

import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author louwenliang
 */
@Data
public class ResultBody<T> implements Serializable, ResponseCode {

    @Serial
    private static final long serialVersionUID = -6190689122701100762L;


    /**
     * 返回状态码
     */

    Integer code;

    /**
     * 返回实体
     */
    T data;

    /**
     * 错误信息
     */
    String message;

    public ResultBody() {

    }

    public ResultBody<T> msg(String msg) {
        this.message = msg;
        return this;
    }

    public ResultBody<T> code(Integer code) {
        this.code = code;
        return this;
    }

    public ResultBody<T> data(T data) {
        this.data = data;
        return this;
    }

    public static <T> ResultBody<T> ok() {
        return new ResultBody<T>().code(ResponseCodeEnum.SUCCESS.getCode()).msg(ResponseCodeEnum.SUCCESS.getMessage());
    }

    public static <T> ResultBody<T> error() {
        return new ResultBody<T>().code(ResponseCodeEnum.ERROR.getCode()).msg(ResponseCodeEnum.ERROR.getMessage());
    }

    public static <T> ResultBody<T> error(T data) {
        return new ResultBody<T>().data(data).code(ResponseCodeEnum.ERROR.getCode()).msg(ResponseCodeEnum.ERROR.getMessage());
    }

    public static <T> ResultBody<T> error(ResponseCodeEnum codeEnum) {
        return new ResultBody<T>().code(codeEnum.getCode()).msg(codeEnum.getMessage());
    }

    public static <T> ResultBody<T> error(String msg) {
        return new ResultBody<T>().code(ResponseCodeEnum.ERROR.getCode()).msg(msg);
    }

    public static <T> ResultBody<T> message(String msg) {
        return new ResultBody<T>().code(ResponseCodeEnum.ERROR.getCode()).msg(msg);
    }

    public static <T> ResultBody<T> ok(T data) {
        return new ResultBody<T>().data(data).code(ResponseCodeEnum.SUCCESS.getCode()).msg(ResponseCodeEnum.SUCCESS.getMessage());
    }
}
